﻿#region copyright
//<copyright>
// Copyright(C) 2012 TrackerRealm Corporation
// This file is part of the open source project - Jazz. http://jazz.codeplex.com
// 
// Jazz is open software: you can redistribute it and/or modify it 
// under the terms of the GNU Affero General Public License (AGPL) as published by 
// the Free Software Foundation, version 3 of the License.
// 
// Jazz is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty 
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
//  See the GNU Affero General Public License (AGPL) for more details.
// 
// You should have received a copy of the GNU General Public 
// License along with Jazz.  If not, see <http://www.gnu.org/licenses/>.
//
// REMOVAL OF THIS NOTICE IS VIOLATION OF THE COPYRIGHT. 
//</copyright>
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;

namespace TrackerRealm.Jazz.Client
{
    /// <summary>
    /// 
    /// </summary>
    public static class ExtensionMethods
    {
        //
        // Extension methods
        //
        #region IsWorkspacePublic
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ws"></param>
        /// <returns></returns>
        public static bool IsWorkspacePublic(this jWorkspaceBase ws)
        {
            if (ws == null) return false;
            if (ws.ClientNexus == null) return false;
            return (ws == ws.ClientNexus._PublicWorkspace);
        }
        #endregion
        #region Empty
        /// <summary>
        /// Returns the empty object for the specified class.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="nexus"></param>
        /// <returns></returns>
        public static T Empty<T>(this ClientNexusBase nexus) where T: jObject
        {
            return (T)nexus.Empty(typeof(T));
        }
        /// <summary>
        /// Returns the empty object for the specified class.
        /// </summary>
        /// <param name="nexus"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public static jObject Empty(this ClientNexusBase nexus, Type t)
        {
            if (!nexus.Cache.JClasses.Contains(t))
                try
                {
                    nexus.Bind(t);
                }
                catch (Exception ex)
                {
                    throw new ArgumentException(string.Format("Need to created an empty object for {0}; however was unable to create.", t.FullName), ex);
                }
            return nexus.Cache.JClasses.Get(t).EmptyInstance;
        }
        #endregion
    }
}
